﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LastoRevival.Service.Contracts;
using System.ServiceModel;
using LastoRevival.Service.Contracts.Models;
using LastoRevival.ForumCode;
using mdryden.Core.Security;
using LastoRevival.Service.Implementations.Settings;

namespace LastoRevival.Service.Implementations
{
	[ServiceBehavior(Name = "LastoRevivalService", Namespace = "http://service.ihateapple.com")]
	public class LastoRevivalService : ILastoRevivalService
	{
		private SimpleHash GetSimpleHash()
		{
			return new SimpleHash(ServiceSettings.GetInstance().HashType);
		}

		#region sub services
		private AdminService GetAdminService()
		{
			return new AdminService(GetUserService());
		}
		
		private ForumService GetForumService()
		{
			return new ForumService(GetUserService());
		}

		private PostService GetPostService()
		{
			var config = new BBCodeConfiguration();
			var codes = config.GetCodesAsList();
			var parser = new BBCodeParserExtension(codes);

			return new PostService(parser, GetUserService(), config.GetCodes());
		}

		private UserService GetUserService()
		{
			return new UserService(GetSimpleHash());
		}
		#endregion


		public LoginAttemptResult Login(Guid loginToken, string username, string password, bool rememberMe)
		{
			return GetUserService().Login(loginToken, username, password, rememberMe);
		}

		public void Logout(Guid loginToken)
		{
			GetUserService().Logout(loginToken);
		}

		public IEnumerable<PreviewForumModel> GetVisibleForums(Guid loginToken)
		{
			return GetForumService().GetVisibleForums(loginToken);
		}

		public ViewForumModel GetForumAndThreads(Guid loginToken, int forumID, int currentPage, int pageSize)
		{
			return GetForumService().GetForumAndThreads(loginToken, forumID, currentPage, pageSize);
		}

		public PreviewForumModel GetForum(Guid loginToken, int forumID)
		{
			return GetForumService().GetForum(loginToken, forumID);
		}

		public int CreatePost(Guid loginToken, CreatePostModel post)
		{
			return GetPostService().CreatePost(loginToken, post);
		}

		public void UpdatePost(Guid loginToken, EditPostModel post)
		{
			GetPostService().UpdatePost(loginToken, post);
		}

		public ViewPostModel GetPostForViewing(Guid loginToken, int postID)
		{
			return GetPostService().GetPostForViewing(loginToken, postID);
		}

		public EditPostModel GetPostForEditing(Guid loginToken, int postID)
		{
			return GetPostService().GetPostForEditing(loginToken, postID);
		}

		public CreateUserAttemptResult CreateUser(Guid loginToken, CreateUserModel user)
		{
			return GetUserService().CreateUser(loginToken, user);
		}
		
		public UserProfile GetActiveProfile(Guid loginToken)
		{
			return GetUserService().GetActiveProfile(loginToken);
		}
		
		public ViewUserModel GetPublicProfile(Guid loginToken, string username)
		{
			return GetUserService().GetPublicProfile(loginToken, username);
		}

		public EditProfileModel GetProfileForEdit(Guid loginToken, string username)
		{
			return GetUserService().GetProfileForEdit(loginToken, username);
		}

		public void UpdateProfile(Guid loginToken, EditProfileModel user)
		{
			GetUserService().UpdateProfile(loginToken, user);
		}
		
		public bool IsAuthenticated(Guid loginToken)
		{
			return GetUserService().IsAuthenticated(loginToken);
		}

		public string GetUsername(Guid loginToken, int userID)
		{
			return GetUserService().GetUsername(loginToken, userID);
		}
		
		public BBTagReference[] GetForumCodes(Guid loginToken)
		{
			return GetPostService().GetForumCodes(loginToken);
		}
		
		public int CreateReply(Guid loginToken, CreateReplyModel post)
		{
			return GetPostService().CreateReply(loginToken, post);
		}

		public int CreateForum(Guid loginToken, CreateForumModel forum)
		{
			return GetAdminService().CreateForum(loginToken, forum);
		}

		public void DeleteForum(Guid loginToken, int forumID)
		{
			GetAdminService().DeleteForum(loginToken, forumID);
		}

		public void EditForum(Guid loginToken, EditForumModel forum)
		{
			GetAdminService().EditForum(loginToken, forum);
		}

		public ViewForumAccessModel GetForumAccessPermissions(Guid loginToken, int forumID)
		{
			return GetAdminService().GetForumAccessPermissions(loginToken, forumID);
		}

		public void UpdateForumAccessPermissions(Guid loginToken, UpdateGroupPermissionsModel access)
		{
			GetAdminService().UpdateForumAccessPermissions(loginToken, access);
		}

		public EditForumModel GetForumForEdit(Guid loginToken, int forumID)
		{
			return GetAdminService().GetForumForEdit(loginToken, forumID);
		}
		
		public void UpdateForum(Guid loginToken, EditForumModel forum)
		{
			GetAdminService().UpdateForum(loginToken, forum);
		}
		
		public void ChangeLockState(Guid loginToken, int postId, bool locked)
		{
			GetPostService().ChangeLockState(loginToken, postId, locked);
		}

		public bool VerifyUser(Guid loginToken, VerifyUserModel user)
		{
			return GetUserService().VerifyUser(loginToken, user);
		}

		public void SendVerificationEmail(Guid loginToken, string username, string messageBodyFormat)
		{
			GetUserService().SendVerificationEmail(loginToken, username, messageBodyFormat);
		}

		public IEnumerable<OnlineUser> GetOnlineUsers(Guid loginToken)
		{
			return GetUserService().GetOnlineUsers(loginToken);
		}
	}
}
